package cl.orange.portal.utils.email;

import com.google.common.base.Joiner;
import com.liferay.mail.service.MailServiceUtil;
import com.liferay.portal.kernel.mail.MailMessage;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.velocity.app.VelocityEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
import org.springframework.ui.velocity.VelocityEngineUtils;
import org.springframework.util.Assert;

@Service
public class EmailServiceImpl implements EmailService {

    private static final Logger LOGGER = LoggerFactory.getLogger(EmailServiceImpl.class);
    @Resource(name = "javaMailSender")
    private JavaMailSenderImpl javaMailSender;
    @Resource(name = "velocityEngine")
    private VelocityEngine velocityEngine;

    @Override
    public <T> void sendMail(final T data, final String templateName, final String subject, final String sendTo) {
        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            @Override
            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
                Map model = new HashMap<String, T>();
                model.put("datos", data);
                String emailText = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateName, "UTF-8", model);
                String[] toMailList = new String[]{sendTo};
                message.setTo(toMailList);
                message.setFrom(javaMailSender.getUsername());
                message.setSubject(subject);
                message.setText(emailText, true);
            }
        };
        this.javaMailSender.send(preparator);
    }

    @Override
    public <T> void sendMailNative(T data, String templateName, String subject, String sendTo) throws AddressException {
        Map model = new HashMap<String, T>();
        model.put("datos", data);
        String emailText = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateName, "UTF-8", model);
        MailMessage mailMessage = new MailMessage(new InternetAddress(javaMailSender.getUsername()), new InternetAddress(sendTo), subject, emailText, true);
        MailServiceUtil.sendEmail(mailMessage);
    }

    @Override
    public <T> void sendMailNative(T data, String templateName, String subject, String sendTo, String... sendToCC) throws AddressException {
        Assert.hasText(sendToCC.toString());
        Map model = new HashMap<String, T>();
        model.put("datos", data);
        String emailText = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateName, "UTF-8", model);
        String sendToCCList = Joiner.on(",").join(sendToCC);
        MailMessage mailMessage = new MailMessage(new InternetAddress(javaMailSender.getUsername()), new InternetAddress(sendTo), subject, emailText, true);
        mailMessage.setCC(InternetAddress.parse(sendToCCList));
        MailServiceUtil.sendEmail(mailMessage);
    }

}
